package com.kuang.controller;

import com.github.pagehelper.PageInfo;

import com.kuang.entity.*;
import com.kuang.service.UserService;
import com.kuang.util.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;

    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public ResultModel<SysUser> info() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        SysUser sysUser = userService.selectBySysUser(username);
        return ResultModel.success(sysUser);
    }

    /**
     * 根据id 修改用户对象
     */
    @PutMapping("/update")
    public ResultModel<SysUser> setStudentInfoId(@RequestBody SysUser sysUser) {
        userService.updateByPrimaryKeySelective(sysUser);
        return ResultModel.success();
    }

    /**
     * 根据 id 修改用户密码
     */
    @PutMapping("/passwordById")
    public ResultModel PasswordById(@RequestBody SysUser sysUser) {
        String str = userService.PasswordByID(sysUser);
        return ResultModel.success(str);
    }


    /**
     * 根据 id 和原密码 修改用户密码
     */
    @PutMapping("/password")
    public ResultModel setPassword(@RequestBody SysUser sysUser) {
        String result = userService.updatePasswordByID(sysUser);
        if (result.equals("原密码错误")) {
            return ResultModel.error(201,"原密码错误2");
        }
        else {
            return ResultModel.success(result);
        }
    }

    /**
     * 注册用户
     */
    @PostMapping("/register")
    public ResultModel<SysUser> register(@RequestBody SysUser sysUser) {
        userService.addSysUser(sysUser);
        return ResultModel.success();
    }

    /**
     * 携带token,获得该用户的所有权限,然后再在前端页面显示对应权限的内容
     */
    @GetMapping("/permission")
    public ResultModel<List<String>> getPermission() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<String> permissionValueList = (List<String>) redisTemplate.opsForValue().get(username);
        return ResultModel.success(permissionValueList);
    }

    /**
     * 获取用户的菜单信息
     */
    @GetMapping("/getMenuList")
    public ResultModel<List<SysPermission>> getMenuList(HttpServletRequest request) {
        String token = request.getHeader("token");
        List<SysPermission> list = userService.getMenuList(token);
        return ResultModel.success(list);
    }

    /**
     * 查询所有用户信息
     */
    @GetMapping("/all")
    public ResultModel<SysUser> getUserAll() {
        List<SysUser> list = userService.selectUserAll();
        return ResultModel.success(list);
    }

    /**
     * 新增用户
     **/
    @PostMapping("/addUser")
    public ResultModel<SysUser> save(@RequestBody SysUser sysUser) {
        userService.addSysUser(sysUser);
        return ResultModel.success();
    }

    /**
     * 定义查询学院、专业和用户的方法: 查询department表 获得表中数据
     **/
    @GetMapping("/department")
    public ResultModel<Department> getAllDepartments() {
        List<Department> data = userService.getAllDepartments();
        return ResultModel.success(data);
    }
    /**
     * 定义查询学院、专业和用户的方法:根据departmentId 查询major表
     * */
    @GetMapping("/major/department/{departmentId}")
    public ResultModel<Major> getMajorsByDepartmentId(@PathVariable Long departmentId) {
        List<Major> data = userService.getMajorsByDepartmentId(departmentId);
        return ResultModel.success(data);
    }

    /**
     * 定义查询学院、专业和用户的方法:根据majorId查user所有
     */
    @GetMapping("/major/{majorId}")
    public ResultModel<Major> getSysUsersByMajorId(@PathVariable Long majorId) {
        List<SysUser> data = userService.getSysUsersByMajorId(majorId);
        return ResultModel.success(data);
    }

}